package com.feri.rbacsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.rbacsys.entity.constant.Statuse;
import com.feri.rbacsys.dao.DepositoryDao;
import com.feri.rbacsys.dao.ExportOrderDao;
import com.feri.rbacsys.entity.Check;
import com.feri.rbacsys.entity.ExportOrder;
import com.feri.rbacsys.entity.Order;
import com.feri.rbacsys.entity.Torder;
import com.feri.rbacsys.mapper.OrderMapper;
import com.feri.rbacsys.mapper.TorderMapper;
import com.feri.rbacsys.security.entity.LoginUser;
import com.feri.rbacsys.service.intf.CheckService;
import com.feri.rbacsys.mapper.CheckMapper;
import com.feri.rbacsys.vo.R;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author 24122
* @description 针对表【t_check】的数据库操作Service实现
* @createDate 2023-03-28 09:09:55
*/
@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check>
    implements CheckService{
    @Resource
    OrderMapper mapper;
    @Resource
    CheckMapper checkMapper;
    @Resource
    DepositoryDao depositoryDao;
    @Resource
    ExportOrderDao exportOrderDao;
    @Resource
    TorderMapper torderMapper;
    @Resource
    StringRedisTemplate template;
    @Override
    @Transactional
    public R checkOrder(Integer id) {
        //id为订单id
        Order order = mapper.selectById(id);
        if(order==null){
            return R.fail("订单已不存在");
        }
        int count = depositoryDao.getCount(order.getPid());
        if(order.getCount()>count){
            return R.fail("库存不足");
        }
        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.set("flag",1);
        wrapper.eq("id",id);
        mapper.update(null,wrapper);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    LoginUser o = (LoginUser) authentication.getPrincipal();
        Integer uid = o.getUser().getId();
        Check check = new Check();
        check.setCid(uid);
        check.setOid(order.getId());
        check.setType(0);
        checkMapper.insert(check);
        ExportOrder exportOrder = new ExportOrder(order.getOrderNum());
        exportOrder.setAmount(order.getCount());
        exportOrder.setPrice(order.getPrice());
        exportOrder.setProductId(order.getPid());
        exportOrderDao.insert(exportOrder);
        return R.ok("审核成功");
    }

    @Override
    public R checkTorder(Integer tid) {
        Torder torder = torderMapper.selectById(tid);
        Order order = mapper.selectById(torder.getOid());
        QueryWrapper<ExportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_num",order.getOrderNum());
        queryWrapper.eq("type",0);
        ExportOrder exportOrder = exportOrderDao.selectOne(queryWrapper);
        if(exportOrder==null){
            QueryWrapper<Check> wrapper = new QueryWrapper<>();
            wrapper.eq("type",1);
            wrapper.eq("oid",tid);
            Check check = checkMapper.selectOne(wrapper);
            if(check==null){
                template.opsForValue().set("statuse", String.valueOf(Statuse.未审核.getValue()));
                //未审核
            }
            return R.ok("可以退款");
        }else {
            Date exportDate = exportOrder.getExportDate();
            if(exportDate==null){
                //已审核未发货
                template.opsForValue().set("statuse", String.valueOf(Statuse.已审核未发货.getValue()));
                return R.ok("可以退款");
            }else {
                long diff = System.currentTimeMillis()-exportDate.getTime();
                // 计算差多少天
                long day = diff / (1000 * 24 * 60 * 60);
                System.out.println(day);
                if(day<=7){
                    template.opsForValue().set("statuse", String.valueOf(Statuse.已发货.getValue()));
                    return R.ok("可以退款");
                }else{
                    return R.fail("超过期限不予退款");
                }
            }
        }
//        if(torder!=null){
//            //先判断该订单是否审核和发货
//            QueryWrapper<Check> wrapper = new QueryWrapper<>();
//            wrapper.eq("type",1);
//            wrapper.eq("oid",tid);
//            Check check = checkMapper.selectOne(wrapper);
//            if(check==null){
//
//            }
//        }
        //return R.fail("没有该退款订单");
    }
}




